package com.sensorfriendly.ui;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

import com.sensorfriendly.AppConstants;
import com.sensorfriendly.AppContext;
import com.sensorfriendly.R;
import com.sensorfriendly.model.Scenario;
import com.sensorfriendly.provider.impl.ScenarioDBImpl;
import com.sensorfriendly.util.ImageUtil;

public class EditScenarioDialog extends Activity implements OnClickListener{

	private IconAdapter iconAdapter;
	private GridView dashBoard;
	private String scenario;
	private EditText nameEditText;
	private String selectedPath;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_edit_scenario);
		Bundle extra = this.getIntent().getExtras();
		scenario = extra.getString(AppConstants.CATEGORY);
		nameEditText = (EditText) findViewById(R.id.scenario_dialog_edit_name);
		if(scenario != null){
			nameEditText.setText(scenario);
			setTitle(scenario);
		}
		else{
			setTitle("Adding New Scenario");
		}
		
		((Button)findViewById(R.id.scenario_dialog_confirm_btn)).setOnClickListener(this);
		((Button)findViewById(R.id.scenario_dialog_cancel_btn)).setOnClickListener(this);

		initializeDashBoard();
	}
	
	
	/**
	 * fill the available icons into the grid view.
	 */
	private void initializeDashBoard() {
		List<String> iconPaths = new ArrayList<String>();
		List<Bitmap> icons = new ArrayList<Bitmap>();
		
		String[] list = null;
		try {
			list = getAssets().list("icons");
		} catch (IOException e) {
			// TODO SENSOR handle case icons DNE
			e.printStackTrace();
		}
		//creating a list of icons.
		if (list != null) {
			for (String iconName : list) {
				InputStream is = null;
				try {
					is = getAssets().open("icons/" + iconName);
				} catch (IOException e) {
					// TODO SENSOR handle image loading failed
					e.printStackTrace();
				}
				if(is !=null){
					//loading images
					final BitmapFactory.Options options = new BitmapFactory.Options();
					options.inJustDecodeBounds = true;
					BitmapFactory.decodeStream(is, null, options);
					int reqWidth = 80;
					int reqHeight = 80;
					options.inSampleSize = ImageUtil.calculateInSampleSize(options, reqWidth, reqHeight);
					// Decode bitmap with inSampleSize set
					options.inJustDecodeBounds = false;
					Bitmap image = BitmapFactory.decodeStream(is,null, options);
					iconPaths.add(iconName);
					icons.add(image);
				}
			}

		}
		dashBoard = (GridView) findViewById(R.id.icons_dashboard);
		iconAdapter = new IconAdapter(this, icons, iconPaths);
		dashBoard.setAdapter(iconAdapter);
		dashBoard.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				
				selectedPath = (String)v.getTag();
				iconAdapter.setSelectedIconIndex(position);
				iconAdapter.notifyDataSetChanged();
				dashBoard.setAdapter(iconAdapter);
				
			}
			
		});
	}
	
	public void onPause(){
		super.onPause();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//commit all changes when user confirms.
		case R.id.scenario_dialog_confirm_btn:
			AppContext app = (AppContext) this.getApplication();
			String value = nameEditText.getText().toString();
			if(value != null && value.length()>0){
				Scenario newScenario = (new ScenarioDBImpl(this).getScenario(scenario));
				newScenario.setName(value);
				newScenario.setIconPath("icons/"+selectedPath);
				app.editScenario(scenario, newScenario);				
				this.finish();
			}
			else{
				Toast.makeText(this, "Please enter a valid name", Toast.LENGTH_LONG).show();
			}
			
			break;
		
		case R.id.scenario_dialog_cancel_btn:
			
			this.finish();
			break;
		}
		
	}
	
	
	
	
}
